package com.ayoka.parenting.ui.coach.studentmanager;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.CareStudentAdapter;
import com.ayoka.parenting.adapter.ManagerStudentAdapter;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.bean.StudentBean;
import com.ayoka.parenting.databinding.FragmentBaseRecyclerviewBinding;
import com.ayoka.parenting.mvp.MVPBaseRecyclerViewFragment;
import com.ayoka.parenting.ui.coach.studentdetail.StudentDetailActivity;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.dialog.DialogHelper;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GrownFragment.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class StudentManagerListFragment extends MVPBaseRecyclerViewFragment<StudentBean, FragmentBaseRecyclerviewBinding, StudentManagerContract.View, StudentManagerPresenter> implements StudentManagerContract.View {

    public static StudentManagerListFragment newInstance(int managerType) {
        StudentManagerListFragment fragment = new StudentManagerListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("ManagerType", managerType);
        fragment.setArguments(bundle);
        return fragment;
    }

    private int managerType;

    @Override
    protected void configBundleArguments() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            managerType = bundle.getInt("ManagerType", Constants.ManagerType.CARE);
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_base_recyclerview;
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<StudentBean, BaseViewHolder> getAdapter() {
        BaseQuickAdapter adapter = null;
        if (managerType == Constants.ManagerType.CARE) {
            adapter = new CareStudentAdapter();
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    final StudentBean studentBean = (StudentBean) adapter.getItem(position);
                    StudentDetailActivity.start(getActivity(), studentBean.StudentUid);
                }
            });
        } else {
            adapter = new ManagerStudentAdapter();
            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    final StudentBean studentBean = (StudentBean) adapter.getItem(position);
                    if (view.getId() == R.id.btn_release) {
                        DialogHelper.getOkCancelDialog(getActivity(), "提示", "确定要解除与该学员的关系吗？", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                if (mPresenter != null) {
                                    mPresenter.releaseStudent(studentBean.StudentUid);
                                }
                            }
                        });
                    }
                }
            });
        }
        return adapter;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        if (mPresenter != null) {
            mPresenter.getStudentListData(isShowWaitDialog, managerType, pageNo, pageSize);
        }
    }

    @Override
    protected void initData() {
        getData(true);
    }

    @Override
    protected StudentManagerPresenter getPresenter() {
        return new StudentManagerPresenter();
    }


    @Override
    public void configStudentListData(PagerBean<StudentBean> data) {
        if (data == null) return;
        setLoadMoreLayout(data.list, data.totalPage);
    }
}
